<template>
  <s-layout title="购物车" tabbar="/pages/index/cart" :tablei="state.tablei" :bgStyle="{ color: '#fff' }">
    <s-empty
      v-if="state.list.length === 0"
      text="购物车空空如也,快去逛逛吧~"
      icon="/static/cart-empty.png"
    />
    <view class="quguang"  v-if="state.list.length === 0" @click="getquguang">去逛逛</view>

    <!-- 头部 -->
    <view class="cart-box ss-flex ss-flex-col ss-row-between" v-if="state.list.length">
      <view class="cart-header ss-flex ss-col-center ss-row-between ss-p-x-30">
        <!-- <view class="header-left ss-flex ss-col-center ss-font-26">
          共
          <text class="goods-number ui-TC-Main ss-flex">{{ state.list.length }}</text>
          件商品
        </view> -->
        <view class="changview">
          <view class="changitem cactive" @click="onTabsChange(0)">
            <text class="goubot">购物车</text>
            <!-- <text class="botxian"></text> -->
          </view>
          <view class="changitem" @click="onTabsChange(1)">
            <text class="goubot">我常买</text>
          </view>
        </view>

        <view class="header-right" style="display: flex;flex-direction: row;align-items: center;">
          <button class="ss-reset-button" style="margin-right: 30rpx;" @tap="getqingkong">
            清空
          </button>

          <button v-if="state.editMode" class="ss-reset-button" @tap="state.editMode = false">
            取消
          </button>
          <button v-else class="ss-reset-button ui-TC-Main" @tap="state.editMode = true">
            编辑
          </button>
        </view>
      </view>
      <!-- 内容 -->
      <view class="cart-content ss-flex-1 ss-p-x-30 ss-m-b-40">
        <view class="goods-box ss-r-10 ss-m-b-14" v-for="item in state.list" :key="item.id">
          <view class="ss-flex ss-col-center">
            <label class="check-box ss-flex ss-col-center ss-p-l-10" @tap="onSelectSingle(item.id)">
              <radio
                :checked="state.selectedIds.includes(item.id)"
                color="var(--ui-BG-Main)"
                style="transform: scale(0.8)"
                @tap.stop="onSelectSingle(item.id)"
              />
            </label>
            <s-goods-item
              :title="item.goods.title"
              :img="item.sku_price.image || item.goods.image"
              :price="item.sku_price.price"
              :skuText="item.sku_price.goods_sku_text"
              priceColor="#FF3000"
              :titleWidth="400"
            >
              <template v-if="!state.editMode" v-slot:tool>
                <su-number-box
                  :min="0"
                  :max="item.sku_price.stock"
                  :step="1"
                  v-model="item.goods_num"
                  @change="onNumberChange($event, item)"
                ></su-number-box>
              </template>
            </s-goods-item>
          </view>
        </view>
      </view>
      <!-- 底部 -->
      <su-fixed bottom :val="48" placeholder v-if="state.list.length > 0" :isInset="false">
        <view class="cart-footer ss-flex ss-col-center ss-row-between ss-p-x-30 border-bottom">
          <view class="footer-left ss-flex ss-col-center">
            <label class="check-box ss-flex ss-col-center ss-p-r-30" @tap="onSelectAll" ref="myButton">
              <radio
                :checked="state.isAllSelected"
                color="var(--ui-BG-Main)"
                style="transform: scale(0.8)"
                @tap.stop="onSelectAll"
              />
              <view class="ss-m-l-8"> 全选 </view>
            </label>
            <!-- <view class="header-left ss-flex ss-col-center ss-font-26" style="margin-right: 10rpx;">
              共
              <text class="goods-number ui-TC-Main ss-flex">{{ state.list.length }}</text>
              件商品
            </view> -->

            <text class="">合计:</text>
            <view class="text-price price-text">
              {{ state.totalPriceSelected }}
            </view>
          </view>
          <view class="footer-right">
            <button
              v-if="state.editMode"
              class="ss-reset-button ui-BG-Main-Gradient pay-btn ui-Shadow-Main"
              @tap="onDelete"
            >
              删除
            </button>
            <button
              v-else
              class="ss-reset-button ui-BG-Main-Gradient pay-btn ui-Shadow-Main"
              @tap="onConfirm"
            >
              去结算
              <!-- {{ state.selectedIds?.length ? `(${state.selectedIds.length}件)` : '' }} -->
              {{ state.totalcount ? `(共${state.totalcount}件)` : '' }}
            </button>
          </view>
        </view>
      </su-fixed>
    </view>
  </s-layout>
</template>

<script setup>
  import sheep from '@/sheep';
  import { computed, reactive, unref,ref } from 'vue';
  import { onShow } from '@dcloudio/uni-app';
  const sys_navBar = sheep.$platform.navbar;
  const cart = sheep.$store('cart');
  const tabMaps = [
    {
      name: '购物车',
      value: 'car',
    },
    {
      name: '我常买',
      value: 'unpaid',
    }
  ];
  const state = reactive({
    editMode: false,
    list: computed(() => cart.list),
    selectedList: [],
    selectedIds: computed(() => cart.selectedIds),
    isAllSelected: computed(() => cart.isAllSelected),
    totalPriceSelected: computed(() => cart.totalPriceSelected),
    tablei:0,
    totalcount:computed(() => cart.xuantotal),
    currentTab: 0,
  });
  // 切换选项卡
  function onTabsChange(e) {
    console.log(e)
    if(e == 1){
      sheep.$router.go('/pages/goods/Frequent')
    }
    // if (state.currentTab === e.index) return;
    

    // state.currentTab = e.index;
  }

  // 单选选中
  function onSelectSingle(id) {
    cart.selectSingle(id);
  }
  // 全选
  function onSelectAll() {
    cart.selectAll(!state.isAllSelected);
    console.log(!state.isAllSelected)
  }
  function onSelectAlltwo() {
    cart.selectAll(true);
  }
  function getqingkong(){
    //清空
    uni.showModal({
      title: '提示',
      content: '确认清空购物车？',
      success: function (res) {
        if (res.confirm) {
          let parms = state.list
          // console.log(state.list)
          let newparms = []
          parms.map((item) => {
            newparms.push(item.goods_id);
          });
          // console.log(newparms)
          uni.$emit('SELECT_HomeList', {//处理首页列表加购数量发送通知
            goods_id:newparms,
            type:'2',//1编辑 2删除
          });
          cart.delete(state.selectedIds);
          state.tablei = Number(state.tablei)+1
        }
      },
    });
  }
 

  // 结算
  function onConfirm() {
    let goods_list = [];
    state.selectedList = state.list.filter((item) => state.selectedIds.includes(item.id));
    state.selectedList.map((item) => {
      goods_list.push({
        goods_id: item.goods_id,
        goods_num: item.goods_num,
        goods_sku_price_id: item.goods_sku_price_id,
      });
    });
    if (goods_list.length === 0) {
      sheep.$helper.toast('请选择商品');
      return;
    }
    sheep.$router.go('/pages/order/confirm', {
      data: JSON.stringify({
        order_type: 'goods',
        goods_list,
        from: 'cart',
      }),
    });
    state.tablei = Number(state.tablei)+1
  }
  function getquguang() {
    uni.switchTab({ url: '/pages/index/index' })
  }

  function onNumberChange(e, cartItem) {
    if (e === 0) {
      cart.delete(cartItem.id);
      return;
    }
    if(cartItem.goods_num === e) return;
    cartItem.goods_num = e;
    // console.log(e)
    cart.update({
      goods_id: cartItem.goods_id,
      goods_num: e,
      goods_sku_price_id: cartItem.goods_sku_price_id,
    });
    state.tablei = Number(state.tablei)+1
    // console.log(state.tablei)
    // console.log("发送通知")
    let a = []
    a.push(cartItem.goods_id)
    uni.$emit('SELECT_HomeList', {//处理首页列表加购数量发送通知
      goods_id:a,
      type:'1',//1编辑
    });
  }
  async function onDelete() {
    // console.log(state.selectedIds)
    let parms = state.list.filter((item) => state.selectedIds.includes(item.id));
    // console.log(parms)
    let newparms = []
    parms.map((item) => {
      newparms.push(item.goods_id);
    });
    // console.log(newparms)
    uni.$emit('SELECT_HomeList', {//处理首页列表加购数量发送通知
      goods_id:newparms,
      type:'2',//1编辑 2删除
    });
    cart.delete(state.selectedIds);
    state.tablei = Number(state.tablei)+1
    
  }
  onShow(() => {
    setTimeout(function () {
      onSelectAlltwo()
    }, 500);
    
  });
</script>

<style lang="scss" scoped>
  :deep(.ui-fixed) {
    height: 72rpx;
  }

  .cart-box {
    width: 100%;

    .cart-header {
      height: 70rpx;
      background-color: #f6f6f6;
      width: 100%;
      position: fixed;
      left: 0;
      top: v-bind('sys_navBar') rpx;
      z-index: 1000;
      box-sizing: border-box;
    }

    .cart-footer {
      height: 100rpx;
      background-color: #fff;

      .pay-btn {
        width: 200rpx;
        height: 70rpx;
        font-size: 24rpx;
        line-height: 28rpx;
        font-weight: 500;
        border-radius: 40rpx;
      }
    }

    .cart-content {
      margin-top: 70rpx;
      .goods-box {
        background-color: #fff;
      }
    }
  }
  .quguang{
    width: 200rpx;
    height: 70rpx;
    background-color: #fb2d35;
    color: #fff;
    display: flex;
    align-items: center;
    margin: 0 auto;
    justify-content: center;
    color: #fff;
    font-size: 30rpx;
    border-radius: 50rpx;
  }
  .changview{
    display: flex;
    flex-direction: row;
    align-items: center;
  }
  .changitem{
    position: relative;
    margin-right: 30rpx;
    height: 50rpx;
    border-radius: 50rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 20rpx;
    // background-color: #ff6d00;
    border: 1rpx solid #ff6d00;
    .goubot{
      color: #ff6d00;
      font-size: 26rpx;
      font-weight: 400;
      width: 100rpx;
      display: flex;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      // padding: 20rpx 0;
    }
    .botxian{
      display: inline-block;
      width: 60%;
      height: 3rpx;
      background-color: #ff6d00;
      position: absolute;
      bottom: 0;
      left: 20%;
    }
  }
  .cactive{
    background-color: #ff6d00;
    .goubot{
      color: #fff;
      font-weight: 500;
    }
  }
</style>
